package com.bcu.calculus.service.serviceImpl;

import com.bcu.calculus.entity.Message;
import com.bcu.calculus.entity.Total;
import com.bcu.calculus.entity.User;
import com.bcu.calculus.mapper.TotalMapper;
import com.bcu.calculus.service.TotalService;
import com.bcu.calculus.utils.SendUtil;
import com.sun.mail.imap.protocol.MessageSet;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class TotalServiceImpl implements TotalService {

    @Resource
    private TotalMapper totalMapper;

    @Override
    public Message<?> addTotal(Total total) {
        total.setTotalCreateTime(new Date());
        total.setTotalUpdateTime(new Date());
        return totalMapper.insert(total) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message<?> delTotal(String totalScoreId) {
        return totalMapper.deleteByPrimaryKey(totalScoreId) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message<?> updateTotal(Total total) {
        total.setTotalUpdateTime(new Date());
        return totalMapper.updateByPrimaryKeySelective(total)  > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message<List<Total>> selectAll() {
        return Message.success(null).add(totalMapper.selectAll());
    }

    @Override
    public Message<Total> selectOneByPrimaryKey(String totalScoreId) {
        return Message.success(null).add(totalMapper.selectByPrimaryKey(totalScoreId));
    }

    @Override
    public Message<Total> selectOneByUserId(String userId,Integer semId) {
        return Message.success(null).add(totalMapper.selectOneByUserId(userId,semId));
    }

    @Override
    public Message<HashMap<String,Object>> totalCount(Integer totalSemId) {
        HashMap map = new HashMap<String,Object>();
        Double totalAvg = totalMapper.totalAvg(totalSemId);
        Double totalPRate = totalMapper.totalPRate(totalSemId);
        map.put("avgScore",totalAvg);
        map.put("PassRate",totalPRate);
        return Message.success(null).add(map);
    }

    @Override
    public Message<?> pushOneScore(Total total) {
        Total total1 = totalMapper.selectOneByUserId(total.getTotalUserId(),total.getTotalSemId());

        if (total1!=null){
            System.out.print(total1.toString()+"\n");
            System.out.print(total1.getTotalScore().compareTo(total.getTotalScore()));
            if (total1.getTotalScore().compareTo(total.getTotalScore())!=0){
                total.setTotalScoreId(total1.getTotalScoreId());
                total.setTotalUpdateTime(new Date());
                return totalMapper.updateByPrimaryKeySelective(total)  > 0 ? Message.success(null) : Message.fail(null);
            }
        }else {

            //total不存在添加total
            String totalId = "TO" + SendUtil.genSendId();
            total.setTotalScoreId(totalId);
            total.setTotalCreateTime(new Date());
            total.setTotalUpdateTime(new Date());
            return totalMapper.insert(total)> 0 ? Message.success(null) : Message.fail(null);

        }
        return Message.success(null);
    }

    @Override
    public Message<List> totalcount(Integer totalSemId) {
        HashMap map = new HashMap<String,Object>();
        List totalcount1 =totalMapper.totalcount1(totalSemId);
        List totalcount2 =totalMapper.totalcount2(totalSemId);
        List totalcount3 =totalMapper.totalcount3(totalSemId);
        List totalcount4 =totalMapper.totalcount4(totalSemId);
        List totalcount5 =totalMapper.totalcount5(totalSemId);
        map.put("90以上",totalcount1);
        map.put("80-90",totalcount2);
        map.put("70-80",totalcount3);
        map.put("60-70",totalcount4);
        map.put("60以下",totalcount5);
        return Message.success(null).add(map);
    }

    @Override
    public Message<List> totalBranch(Integer totalSemId) {
        HashMap map =new HashMap<String,Object>();
        List totalAvgBranch = totalMapper.totalAvgBranch(totalSemId);
        List totalPRateBranch = totalMapper.totalPRateBranch(totalSemId);
        List totalMaxBranch = totalMapper.totalMaxBranch(totalSemId);
        map.put("totalScoreAverage",totalAvgBranch);
        map.put("totalScorePassRate",totalPRateBranch);
        map.put("totalScoreMaxScore",totalMaxBranch);
        return Message.success(null).add(map);
    }

    @Override
    public Message<List> count(Integer totalSemId) {
        HashMap map = new HashMap<String,Object>();
        Integer count1=totalMapper.count1(totalSemId);
        Integer count2=totalMapper.count2(totalSemId);
        Integer count3=totalMapper.count3(totalSemId);
        Integer count4=totalMapper.count4(totalSemId);
        Integer count5=totalMapper.count5(totalSemId);
        map.put("90以上",count1);
        map.put("80-90",count2);
        map.put("70-80",count3);
        map.put("60-70",count4);
        map.put("60以下",count5);
        return Message.success(null).add(map);
    }

    @Override
    public Message<List> getAllRank(Integer totalSemId) {
        return Message.success(null).add(totalMapper.getAllRank(totalSemId));
    }

    @Override
    public Message<List> getAllBranchRank(Integer userBranch, Integer totalSemId) {
        return Message.success(null).add(totalMapper.getAllBranchRank(userBranch,totalSemId));
    }

    @Override
    public Message<Integer> getOneUserRankInBranch(String userId, Integer branch, Integer totalSemId) {
        int rank = 0;
        List<Total> total = totalMapper.getAllBranchRank(branch,totalSemId);
        for (Total t:total) {
            rank++;
            if (userId.equals(t.getTotalUserId())){
                break;
            }
        }

        return Message.success(null).add(rank);
    }


    @Override
    public Message<Integer> getOneUserRank(String userId,Integer totalSemId) {
        int rank = 0;
        List<Total> total = totalMapper.findAllUserAndInfo(totalSemId);
        for (Total t:total) {
            rank++;
            if (userId.equals(t.getTotalUserId())){
                break;
            }
        }

        return Message.success(null).add(rank);
    }

    @Override
    public Message<Integer> getTotalScore(String totalUserId, Integer totalSemId) {
        return Message.success(null).add(totalMapper.getTotalScore(totalUserId,totalSemId));
    }

}
